﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Drawing;
using System.Collections.Generic;

public partial class Pages_Carrier_TripEdit : System.Web.UI.Page
{
	Int64 TripRule_id { get { return DS.ToInt64(Request.QueryString["Id"]); } }

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			BindControls();
			if ((Session["TripRuleSaved"] ?? string.Empty).ToString() == "SavedGood")
			{
				lbMessage.ForeColor = Color.Green;
				lbMessage.Text = "Данные сохранены успешно";
				Session["TripRuleSaved"] = null;
			}
			if (TripRule_id > 0)
				InitUpdateContext();
		}
	}

	private void InitUpdateContext()
	{
		TripRule tr = DBEntity<TripRule>.Load(TripRule_id);
		if (tr == null)
			Response.Redirect("~/");
		lbBusName.Text = tr.bus.Name;
		hfBusID.Value = tr.Bus_id.ToString();
		lbRouteName.Text = tr.route.Name;
		hfRouteID.Value = tr.Route_id.ToString();
		tbTripTime.Text = tr.Time.ToShortTimeString();
		tbTripStartDT.Text = tr.StartDT.ToShortDateString();
		if (tr.EndDT.HasValue)
			tbTripEndDT.Text = tr.EndDT.Value.ToShortDateString();
		if (tr.Even.HasValue)
		{
			rblEvenOdd.SelectedValue = tr.Even.Value.ToString();
			rblSchedule.SelectedValue = "EvenOdd";
		}
		if (tr.TripRulePeriod_id > 0)
		{
			tbAmount.Text = tr.tripRulePeriod.Amount.ToString();
			tbRest.Text = tr.tripRulePeriod.Rest.ToString();
			tbFirstDT.Text = tr.tripRulePeriod.FirstDT.ToShortDateString();
			rblSchedule.SelectedValue = "Period";
		}
	}

	private void BindControls()
	{
		cblWeekDays.DataSource = DBEntity<WeekDay>.Select();
		cblWeekDays.DataBind();
	}

	protected void OnChangeScheduleElement(object s, EventArgs e)
	{
		switch (rblSchedule.SelectedValue)
		{
			case "EvenOdd": dvEvenOdd.Visible = true;
				dvPeriod.Visible = false;
				dvWeekDays.Visible = false;
				break;
			case "Period": dvEvenOdd.Visible = false;
				dvPeriod.Visible = true;
				dvWeekDays.Visible = false;
				break;
			case "WeekDays": dvEvenOdd.Visible = false;
				dvPeriod.Visible = false;
				dvWeekDays.Visible = true;
				break;
		}
		upSchedule.Update();
	}

	protected void OnTripRuleSave(object s, EventArgs e)
	{
		if (IsValid)
		{
			Int64 tripRulePeriod_id = 0;
			bool IsSuccess = true;
			TripRule tr = DBEntity<TripRule>.Load(TripRule_id) ?? new TripRule();
			tr.bus.Id = DS.ToInt64(hfBusID.Value);
			tr.route.Id = DS.ToInt64(hfRouteID.Value);
			tr.Time = DS.ToDateTime(tbTripTime.Text);
			tr.StartDT = DS.ToDateTime(tbTripStartDT.Text);
			if (!string.IsNullOrEmpty(tbTripEndDT.Text.Trim()))
				tr.EndDT = DS.ToDateTime(tbTripEndDT.Text);
			if (rblSchedule.SelectedValue == "EvenOdd")
			{
				tr.Even = DS.ToBoolean(rblEvenOdd.SelectedValue);
				tripRulePeriod_id = tr.TripRulePeriod_id;
				tr.tripRulePeriod = new TripRulePeriod();
				DBEntity<TripRule>.Exec(tr, "TripRule_EraseWeekDay");
			}
			if (rblSchedule.SelectedValue == "Period")
			{
				TripRulePeriod trp = DBEntity<TripRulePeriod>.Load(tr.TripRulePeriod_id) ?? new TripRulePeriod();
				trp.Amount = DS.ToInt(tbAmount.Text);
				trp.Rest = DS.ToInt(tbRest.Text);
				trp.FirstDT = DS.ToDateTime(tbFirstDT.Text);
				tr.Even = null;
				DBEntity<TripRule>.Exec(tr, "TripRule_EraseWeekDay");
			}
			if (rblSchedule.SelectedValue == "WeekDays")
			{
				foreach (ListItem item in cblWeekDays.Items)
				{
					TripRuleWeekDayM2M trwd = new TripRuleWeekDayM2M();
					trwd.tripRule.Id = tr.Id;
					trwd.weekDay.Id = DS.ToInt64(item.Value);
					trwd.x = DS.ToBoolean(item.Selected);
					IsSuccess = trwd.Merge() && IsSuccess;
				}
				tr.Even = null;
				tripRulePeriod_id = tr.TripRulePeriod_id;
				tr.tripRulePeriod = new TripRulePeriod();
			}
			if (tripRulePeriod_id > 0)
				DBEntity<TripRulePeriod>.Delete(tripRulePeriod_id);
			if (tr.Merge() && IsSuccess)
			{
				if (TripRule_id == 0)
				{
					Session["TripRuleSaved"] = "SavedGood";
					Response.Redirect("~/Pages/Carrier/TripRuleEdit.aspx?Id=" + tr.Id.ToString());
				}
				else
				{
					lbMessage.ForeColor = Color.Green;
					lbMessage.Text = "Данные сохранены успешно";
				}
			}
			else
			{
				lbMessage.ForeColor = Color.Red;
				lbMessage.Text = "При сохранении данных проихошла ошибка";
			}
		}
	}

	protected void OnCancel(object s, EventArgs e)
	{
		Response.Redirect("~/Pages/Carrier/TripRule.aspx");
	}

	protected void OnEditSearch(object s, EventArgs e)
	{
		hfModalAction.Value = (s as LinkButton).CommandName;
		tbSearch.Text = string.Empty;
		rptSearch.DataSource = null;
		rptSearch.DataBind();
		lkbAddSearch.Visible = false;
		MPE.Show();
	}

	protected void OnSearch(object s, EventArgs e)
	{
		if (hfModalAction.Value == "Bus")
		{
			Bus b = new Bus();
			b.Name = tbSearch.Text;
			rptSearch.DataSource = DBEntity<Bus>.Find(b);
			lkbAddSearch.Visible = ((rptSearch.DataSource as IList<Bus>).Count == 0);
		}
		if (hfModalAction.Value == "Route")
		{
			Route r = new Route();
			r.Name = tbSearch.Text;
			rptSearch.DataSource = DBEntity<Route>.Find(r);
			lkbAddSearch.Visible = ((rptSearch.DataSource as IList<Route>).Count == 0);
		}
		rptSearch.DataBind();
	}

	protected void OnAddSearch(object s, EventArgs e)
	{
		if (hfModalAction.Value == "Bus")
		{
			Bus b = new Bus();
			b.Name = tbSearch.Text;
			b.PlaceCount = 1;
			if (b.Merge())
			{
				lbBusName.Text = b.Name;
				hfBusID.Value = b.Id.ToString();
			}
		}
		if (hfModalAction.Value == "Route")
		{
			Route r = new Route();
			r.Name = tbSearch.Text;
			r.SourceStation.Id = 1;
			r.TargetStation.Id = 1;
			if (r.Merge())
			{
				lbRouteName.Text = r.Name;
				hfRouteID.Value = r.Id.ToString();
			}
		}
		upRouteBus.Update();
	}

	protected void OnApplySearch(object s, EventArgs e)
	{
		if (hfModalAction.Value == "Bus")
		{
			lbBusName.Text = (s as LinkButton).Text;
			hfBusID.Value = (s as LinkButton).CommandArgument;
		}
		if (hfModalAction.Value == "Route")
		{
			lbRouteName.Text = (s as LinkButton).Text;
			hfRouteID.Value = (s as LinkButton).CommandArgument;
		}
		upRouteBus.Update();
	}
}
